﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StockManagement.ViewModels;
using System.Collections.ObjectModel;
using StockManagement.Helper;

namespace StockManagement.ViewModels
{
    public class MaterialPageViewModel : BaseViewModel
    {
        ObservableCollection<MaterialViewModel> _allMaterialCollection = new ObservableCollection<MaterialViewModel>();
        ObservableCollection<VendorViewModel> _vendorCollection = new ObservableCollection<VendorViewModel>();
        NewMaterialViewModel _newMaterialVM;
        InwardMateriallViewModel _editMaterialVM;
        MaterialGridViewModel _materialGridVM;
        OutwardMaterialViewModel _OutwardMaterialVM;

        # region Constructor
        public MaterialPageViewModel()
        {
            LoadMaterialStock();            
        }
        # endregion

        private void LoadMaterialStock()
        {
            _allMaterialCollection = new ObservableCollection<MaterialViewModel>(DataHelper.GetMaterialsList());
            _vendorCollection = new ObservableCollection<VendorViewModel>(DataHelper.GetVendorList());
            
        }

        public ObservableCollection<MaterialViewModel> MaterialCollection
        {
            get
            {
                return _allMaterialCollection;
            }
        }

        public NewMaterialViewModel NewMaterial
        {
            get 
            {
                if (_newMaterialVM == null)
                {
                    _newMaterialVM = new NewMaterialViewModel();
                    _newMaterialVM.MaterialAdded += new NewMaterialViewModel.MaterialAddedDelegate(_newMaterialVM_MaterialAdded);
                }
                return _newMaterialVM; 
            }
            set
            {
                _newMaterialVM = value;
                OnPropertyChanged("NewMaterial");
            }
        }

        public OutwardMaterialViewModel OutwardMaterialVM
        {
            get
            {
                if (_OutwardMaterialVM == null)
                {
                    _OutwardMaterialVM = new OutwardMaterialViewModel(_allMaterialCollection);
                    _OutwardMaterialVM.MaterialDelivered += new OutwardMaterialViewModel.MaterialDeliveredDelegate(_OutwardMaterialVM_MaterialUpdated);                    
                }
                return _OutwardMaterialVM;
            }
            set
            {
                _OutwardMaterialVM = value;
                OnPropertyChanged("OutwardMaterialVM");
            }
        }

        bool _OutwardMaterialVM_MaterialUpdated(MaterialViewModel material)
        {
            OutwardMaterialVM.UpdateMaterialProperties(material);
            MaterialGrid.RaisePropertyChanged();
            return true;
        }

        void _newMaterialVM_MaterialAdded()
        {
            _allMaterialCollection.Add(_newMaterialVM);
            NewMaterial = null;
        }

        public InwardMateriallViewModel EditMaterial
        {
            get
            {
                if (_editMaterialVM == null)
                {
                    _editMaterialVM = new InwardMateriallViewModel(_allMaterialCollection,_vendorCollection);
                    _editMaterialVM.MaterialDeleted += new MaterialDeletedDelegate(_editMaterialVM_MaterialDeleted);
                    _editMaterialVM.MaterialUpdated += new MaterialUpdatedDelegate(_editMaterialVM_MaterialUpdated);
                }
                return _editMaterialVM;
            }
            set
            {
                _editMaterialVM = value;                
                OnPropertyChanged("EditMaterial");
            }
        }

        void _editMaterialVM_MaterialUpdated()
        {
            var vm = _allMaterialCollection.FirstOrDefault(p => p.MaterialID == _editMaterialVM.SelectedMaterial.MaterialID);
            if (vm == null)
                return;

            vm.MaterialName = _editMaterialVM.MaterialCode;
            vm.MaterialType = _editMaterialVM.MaterialType;
            vm.VendorID = _editMaterialVM.SelectedVendor.VendorID;
            vm.MaterialRate = _editMaterialVM.MaterialRate;
            vm.MaterialVAT = _editMaterialVM.MaterialVAT;
            vm.Quantity = _editMaterialVM.Quantity;
            vm.Date = DateTime.Now;
            vm.OnPropertyChanged("TotalPrice");
        }

        void _editMaterialVM_MaterialDeleted(Guid materialID)
        {
            var vm = _allMaterialCollection.FirstOrDefault(p => p.MaterialID == materialID);
            if (vm == null)
                return;

            _allMaterialCollection.Remove(vm);
        }

        public MaterialGridViewModel MaterialGrid
        {
            get
            {
                if (_materialGridVM == null)
                    _materialGridVM = new MaterialGridViewModel(_allMaterialCollection,_vendorCollection);
                return _materialGridVM;
            }
            set
            {
                _materialGridVM = value;
                OnPropertyChanged("MaterialGrid");
            }
        }
    }
}
